package sk.fhi.client.view;

import java.util.ArrayList;
import java.util.List;

import sk.fhi.client.presenter.ListContactPresenter.Display;
import sk.fhi.shared.model.Contact;

import com.google.gwt.cell.client.ButtonCell;
import com.google.gwt.cell.client.CheckboxCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.DefaultSelectionEventManager;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.MultiSelectionModel;
import com.google.gwt.view.client.ProvidesKey;
import com.google.gwt.view.client.SelectionModel;

public class ListContactView extends Composite implements Display {

	private static ListViewUiBinder uiBinder = GWT
			.create(ListViewUiBinder.class);

	interface ListViewUiBinder extends UiBinder<Widget, ListContactView> {

	}

	public static final ProvidesKey<Contact> KEY_PROVIDER = new ProvidesKey<Contact>() {
		public Object getKey(Contact item) {
			return item == null ? null : item.getId();
		}
	};
	
	ListDataProvider<Contact> dataProvider = new ListDataProvider<Contact>();


	public ListContactView() {
		initWidget(uiBinder.createAndBindUi(this));
		init();
	}
	
	public void init() {
	    final SelectionModel<Contact> selectionModel = new MultiSelectionModel<Contact>(
	        KEY_PROVIDER);
	    table.setSelectionModel(selectionModel,
	        DefaultSelectionEventManager.<Contact> createCheckboxManager());
		    
		 	Column<Contact, Boolean> checkColumn = new Column<Contact, Boolean>(
			        new CheckboxCell(true, false)) {
						@Override
						public Boolean getValue(Contact object) {
							 return selectionModel.isSelected(object);
						}
			    };
		
		
		    TextColumn<Contact> nameColumn = new TextColumn<Contact>() {
		      @Override
		      public String getValue(Contact contact) {
		        return contact.getFirstName();
		      }
		    };
		    nameColumn.setSortable(true);
		    
		    TextColumn<Contact> emailColumn = new TextColumn<Contact>() {
		      @Override
		      public String getValue(Contact contact) {
		        return contact.getEmailAddress();
		      }
		    };
		    
		    
		    ButtonCell PreviewButton= new ButtonCell(); 
		    editButton = new Column<Contact, String>(PreviewButton) {

				@Override
				public String getValue(Contact object) {
					return "Edit";
				}
			};
			
		    
		 // Add the columns.
		    table.addColumn(checkColumn, "");
		    table.addColumn(nameColumn, "Name");
		    table.addColumn(emailColumn, "email");
		    table.addColumn(editButton, "email");
		    
		    
		    
		 // Connect the table to the data provider.
		    dataProvider.addDataDisplay(table);
		    
		    
		    
	}

	Column<Contact, String> editButton;
	
	@UiField
	Button addButton1;

	@UiField
	Button deleteButton1;

	@UiField
	VerticalPanel contentTable;

	@UiField
	HorizontalPanel hPanel;

	@UiField
	CellTable<Contact> table;

	@Override
	public void startProcessing() {
		// TODO Auto-generated method stub

	}

	@Override
	public void stopProcessing() {
		// TODO Auto-generated method stub

	}

	

	@Override
	public CellTable<Contact> getTable() {
		return table;
	}

	@Override
	public void setData(List<Contact> data) {
		dataProvider.setList(data);
		/*for (Contact contact : data) {
			dataProvider.getList().add(contact);
		}*/
	}

	@Override
	public HasClickHandlers getAddButton() {
		return addButton1;
	}

	@Override
	public HasClickHandlers getDeleteButton() {
		return deleteButton1;
	}

	@Override
	public List<Long> getSelectedRows() {
		List<Long> selectedRows = new ArrayList<Long>();
		SelectionModel<Contact> sm = (SelectionModel<Contact>) table.getSelectionModel();
		for (Contact contact : dataProvider.getList()) {
			if (sm.isSelected(contact)) {
				selectedRows.add(contact.getId());
			}
		}
		
//		table.getSelectionModel().
		/*for (int i = 0; i < table.getRowCount(); ++i) {
			TableRowElement e = table.get
			Column<Contact, Boolean> c = (Column<Contact, Boolean>) table.getColumn(0);
			c.ge
			e.get
			CheckBox checkBox = (CheckBox) contactsTable.getWidget(i, 0);
			if (checkBox.getValue()) {
				selectedRows.add(i);
			}
		}*/
		return selectedRows;
	}

	@Override
	public Column<Contact, String> getEditButton() {
		return editButton;
	}

}
